/**
 * Created by python on 19-3-1.
 */
let vm = new Vue({
    el: '#app',
    data: {
        host: HOST,
        user_id: sessionStorage.user_id || localStorage.user_id,
        token: sessionStorage.token || localStorage.token,
        spit: null,
        comm: null,
        content: '',
        error_content: false,
    },
    mounted: function () {
        this.get_spit();
        this.get_spit_comments()
    },
    methods: {
        get_spit: function () {
            let spit_id = get_query_string('id', 3);
            axios.get(this.host + '/spit/' + spit_id + '/', {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
                withCredentials: true
            })
                .then(response => {
                    this.spit = response.data
                    if (this.user_id) {
                        axios.post(this.host+'/browse_histories/', {
                            history_title: this.spit.content,
                            history_address: window.location.href
                        }, {
                            headers: {
                                'Authorization': 'JWT ' + this.token
                            }
                        })
                    }
                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

        get_spit_comments: function () {
            let spit_id = get_query_string('id', 3);
            axios.get(this.host + '/spit/' + 'comments/' + spit_id + '/', {
                headers: {
                    'Authorization': 'JWT ' + this.token
                },
                responseType: 'json',
                withCredentials: true
            })
                .then(response => {
                    console.log(response.data);
                    this.comm = response.data
                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },
        check_comment: function () {

            let len = this.content.length;
            if (len < 2) {
                alert('评论不能为空');
                this.error_content = true
            }
            else {
                this.error_content = false
            }
        },
        submit_comment: function () {
            this.check_comment();
            if (this.error_content == true) {
                return
            }
            let spit_id = get_query_string('id', 3);
            axios.post(this.host + '/spit/submitcomments/', {
                    content: this.content,
                },
                {
                    params: {
                        'user_id': this.user_id,
                        'spit_id': spit_id,
                    },
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                    withCredentials: true
                })
                .then(response => {
                    layer.msg('评论成功');
                    this.content = '';
                    this.get_spit();
                    this.get_spit_comments()

                })
                .catch(error => {
                    layer.msg('评论失败')
                })
        },
        like_comment: function (comment_id) {
            axios.post(this.host + '/spit/commentslike/' + comment_id + '/', {
                },
                {
                    headers: {
                        'Authorization': 'JWT ' + this.token
                    },
                    responseType: 'json',
                    withCredentials: true
                })
                .then(response => {
                    this.get_spit_comments();
                    layer.msg(response.data.msg);
                })
                .catch(error => {
                    layer.msg(error.data.msg);
                })
        }
    }
});